package com.hualan.sfzyproject.controller.admin;

import com.hualan.sfzyproject.domain.CourseReviews;
import com.hualan.sfzyproject.domain.CourseTypes;
import com.hualan.sfzyproject.service.CourseReviewsService;
import com.hualan.sfzyproject.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CourseReviewsController
 * @Description TODO
 * @Author libowen
 * @Date 2025年01月09日 8:46
 * @Version 1.0
 */
@RestController
@RequestMapping("admin/courseReviews")
public class CourseReviewsController {
    @Autowired
    private CourseReviewsService courseReviewsService;
    @PostMapping
    public R add(@RequestBody CourseReviews courseReviews) {
        return courseReviewsService.insert(courseReviews) != 0 ? R.success("添加成功") : R.error("添加失败");
    }

    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable("id") Long id) {
        return courseReviewsService.delete(id) != 0 ? R.success("删除成功") : R.error("删除失败");
    }

    @PutMapping("update/{id}")
    public R update(@RequestBody CourseReviews courseReviews) {
        return courseReviewsService.update(courseReviews) != 0 ? R.success("修改成功") : R.error("修改失败");
    }

    //模糊查询
    @GetMapping("/search")
    public R search(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "5") Integer pageSize) {
        try {
            return R.ok(courseReviewsService.search(keyword, page, pageSize));
        } catch (Exception e) {
            return R.error("搜索失败: " + e.getMessage());
        }
    }

    //批量删除
    @DeleteMapping("/batch")
    public R batchDelete(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return R.error("未选择要删除的课程");
        }
        try {
            boolean result = courseReviewsService.batchDelete(ids);
            return result ? R.success("批量删除成功") : R.error("批量删除失败");
        } catch (Exception e) {
            return R.error("删除失败: " + e.getMessage());
        }
    }
    //课程评价查找全部
    @GetMapping("/detailReviews")
    public R getReviews(Integer page,Integer pageSize) {
        return R.ok(courseReviewsService.selectAll(page,pageSize));
    }

}
